package com.ysstech.regression.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.regression.entity.Usecase;
import com.ysstech.regression.enums.UsecaseEnum;
import com.ysstech.regression.service.UsecaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用例管理 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-03-04
 */
@Controller
@Slf4j
@Api(tags = "用例管理api")
@RequestMapping("/usecase")
public class UsecaseController extends BaseController {

    @Autowired
    private UsecaseService usecaseService;

    @YssPermissions(value = {"regression:usecase"})
    @ApiOperation(value = "跳转用例管理页面")
    @YssLog(module = "用例管理", remark = "跳转用例管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "regression/usecase";
    }


    @YssPermissions(value = {"regression:usecase"})
    @ApiOperation(value = "获取用例管理页面初始值")
    @YssLog(module = "用例管理", remark = "获取用例管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        // 1.处理交易数据 2.生产估值表 3.生产凭证 4.生产ETF台账  5.生产ETF凭证 6.生产ETF估值表
        List<Dicvalue> listType = dicValueService.getCacheDicValueList(UsecaseEnum.USECASE_TYPE.getCode());
        map.put("listType", listType);
        List<Dicvalue> fsetcode = dicValueService.getCacheDicValueList(UsecaseEnum.FSETCODE.getCode());
        map.put("fsetcode", fsetcode);
        List<Dicvalue> tabletype = dicValueService.getCacheDicValueList(UsecaseEnum.TABLETYPE.getCode());
        map.put("tabletype", tabletype);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }


    @YssPermissions(value = {"regression:usecase"})
    @YssLog(module = "用例管理", remark = "查询用例数据")
    @ApiOperation(value = "查询用例数据")
    @RequestMapping(value = "/listUsecase")
    @ResponseBody
    public RestResult listUsecase(Usecase usecase, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("查询用例数据成功",
                usecaseService.queryAllUsecase(usecase, this.getPage(page, rows)));
    }


    @YssPermissions(value = {"regression:usecase:add"})
    @YssLog(module = "用例管理", remark = "添加用例")
    @ApiOperation(value = "添加用例")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(Usecase usecase) throws Exception {
        usecase.setId(DateUtil.generateUUID());
        usecaseService.save(usecase);
        return ResultGenerator.getSuccessResult("添加用例信息成功");
    }


    @YssPermissions(value = {"regression:usecase:edit"})
    @YssLog(module = "用例管理", remark = "修改用例")
    @ApiOperation(value = "修改用例")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(Usecase usecase) throws Exception {
        usecaseService.updateById(usecase);
        return ResultGenerator.getSuccessResult("修改用例信息成功");
    }

    @YssPermissions(value = {"regression:usecase:del"})
    @YssLog(module = "用例管理", remark = "删除用例")
    @ApiOperation(value = "删除用例")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException("获取删除用例信息失败，请稍后重试！");
        }
        List<String> logIds = JSONArray.parseArray(id, String.class);
        usecaseService.removeByIds(logIds);
        return ResultGenerator.getSuccessResult("删除用例信息成功");
    }


    /**
     * @return
     */
    @YssPermissions(value = {"regression:usecase:execute"})
    @YssLog(module = "用例管理", remark = "执行用例")
    @ApiOperation(value = "执行用例")
    @RequestMapping(value = "/executeUsecase")
    @ResponseBody
    public RestResult executeUsecase(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取需要执行的用例信息失败，请稍后重试！");
        }
        List<String> useCaseIds = JSONArray.parseArray(ids, String.class);
        Map<String, Object> map = null;
        for (String id : useCaseIds) {
            Usecase usecase = usecaseService.getById(id);
            if (null == usecase) {
                continue;
            }
            if ("post".equals(usecase.getRequested())) {
                String msg = usecaseService.sendPost(usecase);
                map = (Map) JSON.parse(msg);
            } else {
                String msg = usecaseService.sendGet(usecase);
                map = (Map) JSON.parse(msg);
            }
        }
        return ResultGenerator.getSuccessResult("执行用例成功!", map);
    }


    @YssPermissions(value = {"regression:usecase:upload"})
    @YssLog(module = "用例管理", remark = "导入数据")
    @ApiOperation(value = "导入数据")
    @RequestMapping(value = "/upload")
    @ResponseBody
    public RestResult upload(Usecase usecase) throws Exception {
        Integer num = usecaseService.upload(usecase);
        return ResultGenerator.getSuccessResult("导入数据成功" + num + "条！");
    }


    @YssPermissions(value = {"regression:usecase:view"})
    @ApiOperation(value = "查询差异结果")
    @YssLog(module = "用例管理", remark = "查询差异结果")
    @RequestMapping(value = "/queryData")
    @ResponseBody
    public RestResult queryData(Usecase usecase, String page, String rows) throws Exception {
        return usecaseService.queryData(usecase, this.getPage(page, rows));
    }


    @YssPermissions(value = {"regression:usecase:view"})
    @ApiOperation(value = "获取数据库表字段")
    @YssLog(module = "用例管理", remark = "获取数据库表字段")
    @RequestMapping(value = "/getColumns")
    @ResponseBody
    public RestResult getcolumns(Usecase usecase) throws Exception {
        Map<String, String> map =usecaseService.getTableColumnNames(usecase);
        return ResultGenerator.getSuccessResult("获取查询语句字段成功！", map);
    }
}
